package cm.iccsoft.android.telecomcost.activities;

import java.util.List;

import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.Utility;
import cm.iccsoft.android.telecomcost.entities.VoiceRate;
import cm.iccsoft.android.telecomcost.ui.VoiceRateDialog;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class VoiceRateListActivity extends TelecomcostActivity {

	private ListView list;
	private DatabaseManager db;
	public static final int DIALOG_VOICE_RATE = 1;
	public static final int DIALOG_TIME_PICKER = 2;

	public List<VoiceRate> VoiceRates;

	@Override
	protected Dialog onCreateDialog(int id) {
		Log.i("VoiceRateListActivity", "Creating dialog ohne bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_VOICE_RATE:
			dialog = new VoiceRateDialog(VoiceRateListActivity.this, null);
			break;
		default:
			dialog = null;
		}
		return dialog;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle data) {
		Log.i("VoiceRateListActivity", "Creating dialog with bundle");
		Dialog dialog = null;
		Gson gson = TelecomCost.getGson();
		switch (id) {
		case DIALOG_VOICE_RATE:
			if (data != null) {
				dialog = new VoiceRateDialog(VoiceRateListActivity.this,
						data.containsKey("VoiceRate") ? gson.fromJson(
								data.getString("VoiceRate"), VoiceRate.class)
								: null);
			}
			break;
		default:
			dialog = null;
		}

		if (dialog == null) {
			return onCreateDialog(id);
		}

		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		Log.i("ProviderListActivity", "Preparing dialog with bundle");
		super.onPrepareDialog(id, dialog, args);
		Utility.setDialogLayout(dialog);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		Log.i("ProviderListActivity", "Preparing dialog ohne bundle");
		super.onPrepareDialog(id, dialog);
		Utility.setDialogLayout(dialog);
	}

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("VoiceRateListActivity",
				"Creating the activity " + this.getTitle()
						+ "...setup of global state of the activity");

		super.onCreate(savedInstanceState);
		db = TelecomCost.getDatabaseManager();

		setContentView(R.layout.threshold_list);

		list = (ListView) findViewById(R.id.listViewThreshold);

		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				showSelectedVoiceRate(arg2);
			}
		});

		bindVoiceRates();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("VoiceRateListActivity",
				"creating options menu for VoiceRates : ");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.standard_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_new:
			showVoiceRateDialog();
			return true;
		case R.id.menu_item_delete:
			deleteVoiceRates();
			bindVoiceRates();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	protected void deleteVoiceRates() {
		Log.i("VoiceRateListActivity", "Deleting selected VoiceRates : ");
		int i = 0;
		DatabaseManager db = TelecomCost.getDatabaseManager();

		while (i < VoiceRates.size()) {
			VoiceRate VoiceRate = VoiceRates.get(i);
			if (VoiceRate.isSelected()) {
				// db.deleteVoiceRate(VoiceRate);
			}
			i++;
		}
	}

	protected void showSelectedVoiceRate(int index) {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		Log.i("VoiceRateListActivity",
				"Destroying the activity " + this.getTitle());
		db = null;
		list = null;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		Log.i("VoiceRateListActivity",
				"Pausing the activity " + this.getTitle());
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		Log.i("VoiceRateListActivity",
				"Postcreating the activity " + this.getTitle());
	}

	@Override
	protected void onPostResume() {
		// TODO Auto-generated method stub
		super.onPostResume();
		Log.i("VoiceRateListActivity",
				"PostResuming the activity " + this.getTitle());
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i("VoiceRateListActivity",
				"Restarting the activity " + this.getTitle());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		Log.i("VoiceRateListActivity", "onRestoreInstanceState the activity "
				+ this.getTitle());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("VoiceRateListActivity",
				"Resuming the activity " + this.getTitle());
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		Log.i("VoiceRateListActivity", "onSaveInstanceState the activity "
				+ this.getTitle());
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("VoiceRateListActivity",
				"Starting the activity " + this.getTitle());
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("VoiceRateListActivity",
				"Stopping the activity " + this.getTitle());
	}

	private void showVoiceRateDialog() {
		Log.i("VoiceRateListActivity", "displaying voice rate dialog : ");
		showDialog(DIALOG_VOICE_RATE);
	}

	public void bindVoiceRates() {
		Log.i("VoiceRateListActivity", "binding voice rates");
		// VoiceRates = db.getVoiceRates();
		//
		// list.setAdapter(new VoiceRateAdapter(VoiceRateListActivity.this,
		// R.layout.item_row, (ArrayList<VoiceRate>) VoiceRates,this));
	}

	@Override
	public String toString() {
		return "VoiceRateListActivity";
	}

	public VoiceRate getVoiceRate(String name) {
		String _name;
		int i = 0;
		while (i < VoiceRates.size()) {
			_name = VoiceRates.get(i).getName();
			if (_name.equals(name)) {
				Log.i("VoiceRateListActivity",
						"this VoiceRate already exists...we update it");
				return VoiceRates.get(i);
			}
			i++;
		}

		return null;
	}
}
